package definite.Guide.Book.Chap01_02;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ButtonSample {

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			@Override
			public void run() {
				JFrame frame = new JFrame("Button Sample");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				JButton button = new JButton("Select Me ");

				// define action listener

				ActionListener actionListener = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						System.out.println("Click Me");
					}
				};
				// define mouse listener
				MouseListener mouseListener = new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent mouseEvent) {
						int modifier = mouseEvent.getModifiers();
						if ((modifier & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK)
							System.out.println("Left mouse selected");
						if ((modifier & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK)
							System.out.println("Middle mouse selected");
						if ((modifier & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
							System.out.println("Right mouse selected");
					}

					@Override
					public void mouseReleased(MouseEvent e) {

						if (SwingUtilities.isLeftMouseButton(e))
							System.out.println("Left mouse released");
						if (SwingUtilities.isMiddleMouseButton(e))
							System.out.println("Middle mouse released");
						if (SwingUtilities.isRightMouseButton(e))
							System.out.println("Right mouse realeased");
						System.out.println();
					}
				};
				button.addActionListener(actionListener);
				button.addMouseListener(mouseListener);
				frame.add(button, BorderLayout.SOUTH);
				frame.setSize(300, 400);
				frame.setVisible(true);
			}
		};
		EventQueue.invokeLater(runner);
	}
}
